Instance groups#

In the last lesson, we looked at Compute instances and how to connect to them for different OSes. The Compute Instance is a fundamental unit for all complex architectures or high availability architectures. Let’s look at one of the ways to handle a large load using Compute instance.

Definition#

An instance group is a method to manage a group of instances as a single entity. Instance groups let you organize VM instances or use them in a load-balancing backend service. You can group existing instances or create a group based on an instance template. There are two ways to group instances.

Managed instance group

Managed instance group allows us to have multiple copies of the same instance. A template for an instance is created and the managed instance group creates the specified number of live instances from that template.

This method is used in Autoscaling. We will soon see what autoscaling is but, in short, automatically creating or deleting instances based on the traffic or different metrics is called Autoscaling.

Managed instance group is good for stateless applications where instances are not used for storing any data in them. However, Google Cloud recently launched Instance groups for stateful applications as well. But it is advised to use managed services for stateful applications for reliability.

Unmanaged Instance group

Unmanaged instance group does not use any template and instances having different configuration are grouped together. This type is not used in autoscaling. And also it is recommended to use the managed instance group whenever possible. Maintaining the unmanaged instance group is a tedious task as it includes different types of instances.

Autoscaling#

Autoscaling is a must to have for end-user-facing workloads which get irregular traffic. The different metrics such as CPU usage, memory usage, and any log entries are used to automatically create or delete the instances to meet the user demand and save cost when there is not much load on the servers.

Let’s create a managed instance group and see the autoscaling in action.

Creating an instance group#

We will focus on the managed instance group for stateless applications in this course. To create an instance group, go to,

  • Main menu > Compute Engine > Instance group.

  • Click on the Create Instance Group button. (Keep the selected category of managed instance group for stateless apps.)

A form will be displayed to create an instance group. Let’s understand the fields one by one.

  • Name: You can provide any hyphen-separated alphanumeric name. Let’s use “instance-group-demo” for now.

  • Description: Optional. Describe the purpose of what this group is created or any other information.

  • Location: Choose the location. The single zone will create instances at one location as opposed to multiple locations when multiple zones are selected.

  • Instance Template: To create new VMs immediately, we need to provide an instance template. You can select the existing one or click on Create a new instance template in the dropdown. Since we don’t have any template created click on the Create a new instance template button. This will open the same exact form we used to create a VM.

Click the "Create a new instance template" button.
Click the "Create a new instance template" button.

Google Cloud will use this template to create new VMs in the instance group as needed. Fill the form in the same way we filled in the previous lesson. Choose Ubuntu 18 as OS.

We will make use of the Startup script property this time. We need to check autoscaling for our instance group. The metric we will use is “CPU Usage”. Once the CPU usage of the group cross 60%, a new instance should be created. To achieve this, we need to manipulate the CPU usage. The “Stress” software will help us to do that.

Installing stress application

We will make use of the Startup Script property of instance to install an apache webserver and the stress application.

So, in the form select the Allow HTTP and HTTPS checkboxes. Then in the Management section, in Startup Script copy paste these commands.

sudo apt update -y
sudo apt install -y apache2 stress

sudo stress --cpu 1 --timeout 120
widget

The last command sudo stress --cpu 1 --timeout 120 will spike CPU usage to 100% for 2 minutes.

Keep all the values to default and click “save & continue”.

  • Number of instances: This field will be disabled if we have an active Autoscaling configuration. If no autoscaling configuration is provided then this field takes the number of instances to create in the instance group. Autoscaling configuration is the immediate next section after this field.

  • Autoscaling metrics: Check the available types of metrics by clicking on “Add new metric”. Currently, we can have autoscaling based on 3 types of metrics. The default metric is the CPU utilization which is by default set to 60%.

Auto scaling metrics.
Auto scaling metrics.

Cancel the new metric settings and go with the default CPU utilization metric for now.

  • Predictive autoscaling: Predictive autoscaling analyzes the CPU load and autoscale proactively. This option will be available after 3 days of creating an instance group.

  • Cool down period: Time taken by instance to initialize from boot time until it is ready to serve.

  • Minimum & Maximum number of instances: These fields decide the minimum and the maximum number of instances created in the group when the specified auto scaling metric is exceeded.

Autoscaling takes care of the load on the server, but what if the server itself is facing issues? How would you know that server is healthy?

Auto healing

Auto-healing allows the recreation of VM instances when needed. You can use a health check to recreate a VM instance if the health check finds it unresponsive. If you don’t select a health check, Compute Engine will recreate VM instances only when they’re not running.

Basically, a health check is like an agent who keeps communicating with the instance and expects a response within a specified time period. If no or expected response is not received then the instance is declared unhealthy.

For this demo, we will not use auto-healing. You can have a look at the health check form by clicking on the dropdown and click “create a new health check”.

We will not use the advance creation option so, skip it and click “create”.

The selected region may have a quota limit for the number of instances you can create. If you get an error like Exceeded limit: QUOTA_FOR_INSTANCES try reducing the maximum number of instances to 3 in autoscaling configuration.

New instances#

As soon as the group is created, you will see a new VM is being created. Go to the VM instances list to see that. Wait for a minute and once you see the “green” check, click on the external IP of the instance to see the installed Apache server. Change https:// to http://.

Copy and paste external IP in browser.
Copy and paste external IP in browser.

After 2 mins you will see a new instance is being created. The reason is we have an auto-scaling configuration that checks for the average CPU utilization of the group and if it more than 60% then it creates a new instance to keep the average utilization below 60. After ~10 minutes, the average goes below 60% because the stress program stops the execution. As a result, all the extra instances are deleted and the minimum number of instances which is 1, in this case, is kept.

If no instance is created after 2 mins, SSH into the instance (VM machine) and check system logs to make sure the startup script is executed without any error. You can check logs using cat var/log/syslog command.

Rolling updates and restart/replace#

Click on the newly created instance-group-1. You will get the “Rolling update” & “Rolling restart/replace” options in the header.

Rolling update and rolling restart/replace buttons.
Rolling update and rolling restart/replace buttons.

A rolling update is a gradual update of the instances in the instance group to the target configuration of templates. The rolling update feature helps in updating all the instances in a group or test the new update using canary testing. (Forwarding a little traffic to the new update.)

The rolling restart/replace helps in restarting or replacing the instances in the group. The reason can be a maintenance patch that requires a restart or something like that.

There are different properties for both rolling strategies. The main properties to keep the focus on are:

  • Maximum surge: Maximum number (or percentage) of temporary instances to add while replacing.

  • Maximum unavailable: Maximum instances (number or percentage) that can be offline at the same time while restarting/replacing.

Click on the “Rolling Update” button to see these options.

Maximum surge and unavailable instance.
Maximum surge and unavailable instance.

These properties are also useful with GKE which we will see in the next lesson. So, clean up the instances by deleting the instance group and take a break to revise once what we have learned. The rolling update is good to know but not required as of now. Just make sure you understand the use of Maximum surge and Maximum unavailable to rollout updates effectively.

Instance groups using gcloud#

Instance groups can also be created using the gcloud SDK. If you have created the instance template then run the following command in the terminal.

gcloud compute --project=gcp-headstart-educative-308308 instance-groups managed create instance-group-2 --base-instance-name=instance-group-2 --template=instance-template-1 --size=1 --zone=us-central1-a

The command has all the required parameters and the optional parameters are not provided. The full list of optional parameters can be found here.

Command will create a managed instance group named instance-group-2 using the instance template named instance-template-1. The group will have 1 instance irrespective of the load on the instance. Hence it not an auto-scaled instance group.

To make the above group an auto-scaled instance group, enter the below command.

gcloud compute instance-groups managed \
set-autoscaling instance-group-2 \
--max-num-replicas 3 \
--min-num-replicas 1 \
--target-cpu-utilization 0.60 \
--cool-down-period 120 \
--zone=us-central1-a

This will set the auto-scaling configuration on the instance-group-2. The maximum number of replicas will be 3 in case of more than 60% CPU utilization of the instance group. There will at least 1 instance in case of no-load as well. The instance group will check for CPU utilization of the group after 120 seconds of new instance creation.

Here is the sample output of both commands.


gcpcourseeducative@cloudshell:~ (gcp-headstart-educative-308308)$ gcloud compute --project=gcp-headstart-educative-308308 instance-groups managed create instance-group-2 --base-instance-name=instance-group-2 --template=instance-template-1 --size=1 --zone=us-central1-a
Created [https://www.googleapis.com/compute/v1/projects/gcp-headstart-educative-308308/zones/us-central1-a/instanceGroupManagers/instance-group-2].
NAME  LOCATION  SCOPE  BASE_INSTANCE_NAME  SIZE  TARGET_SIZE  INSTANCE_TEMPLATE AUTOSCALED
instance-group-2  us-central1-a  zone   instance-group-2    0     1    instance-template-1  no


gcpcourseeducative@cloudshell:~ (gcp-headstart-educative-308308)$ gcloud compute instance-groups managed \
> set-autoscaling instance-group-2 \
> --max-num-replicas 3 \
> --min-num-replicas 1 \
> --target-cpu-utilization 0.60 \
> --cool-down-period 120 \
> --zone=us-central1-a
Created [https://www.googleapis.com/compute/v1/projects/gcp-headstart-educative-308308/zones/us-central1-a/autoscalers/instance-group-2-vv1s].
---
autoscalingPolicy:
  coolDownPeriodSec: 120
  cpuUtilization:
    utilizationTarget: 0.6
  maxNumReplicas: 3
  minNumReplicas: 1
  mode: ON
creationTimestamp: '2021-04-10T07:58:53.278-07:00'
id: '707490694353912770'
kind: compute#autoscaler
name: instance-group-2-vv1s
selfLink: https://www.googleapis.com/compute/v1/projects/gcp-headstart-educative-308308/zones/us-central1-a/autoscalers/instance-group-2-vv1s
status: ACTIVE
target: https://www.googleapis.com/compute/v1/projects/gcp-headstart-educative-308308/zones/us-central1-a/instanceGroupManagers/instance-group-2
zone: https://www.googleapis.com/compute/v1/projects/gcp-headstart-educative-308308/zones/us-central1-a
gcpcourseeducative@cloudshell:~ (gcp-headstart-educative-308308)$
Terminal 1
Terminal

Click to Connect...

Connecting to VM Instance

Google Kubernetes Engine